package net.pws.oos.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.pws.common.util.StringUtils;
import net.pws.common.jackson.JsonUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.Area;
import net.pws.oos.biz.query.AreaFinder;
import net.pws.oos.biz.service.AreaService;
import net.pws.oos.web.dto.AreaDto;
import net.pws.oos.web.dto.AreaTreeNodeDto;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/biz/dict/area")
@SuppressWarnings("rawtypes")
public class AreaAction {
    
    @Autowired
    private EntityManager entityManager;
    
    @Autowired
    private AreaFinder areaFinder;
    
    @Autowired
    private AreaService areaService;
    
    @RequestMapping("/provinces.do")
    public String listProvincePage() {
        return "/dict/area/provinces";
    }
    
    @RequestMapping("/province.do")
    public String editProvincePage(String id) {
        return "/dict/area/province";
    }
    
    @RequestMapping("/cities.do")
    public String listCities(String parentId, ModelMap map) {
        Area province = entityManager.find(Area.class, parentId);
        map.put("province", province);
        return "dict/area/cities";
    }
    
    @RequestMapping("/city.do")
    public String editCityPage(String id) {
        return "/dict/area/city";
    }
    
    @RequestMapping("/list.json")
    @ResponseBody
    public Object listDatas(String parentId) {
        List<Area> models = null;
        if (StringUtils.isBlank(parentId)) {
            models = areaFinder.listProvinces();
        }
        else {
            models = areaFinder.listCities(parentId);
        }
        return JsonUtils.succeedMap(AreaDto.from(models));
    }
    
    @RequestMapping("/detail.json")
    @ResponseBody
    public Object detail(String id) {
        if (StringUtils.isBlank(id)) {
            return JsonUtils.failedMap("错误的参数！");
        }
        
        Map area = areaFinder.getArea(id);
        return JsonUtils.succeedMap(AreaDto.from(area));
    }
    
    @RequestMapping("/save.json")
    @ResponseBody
    public Object save(AreaDto dto) {
        Area model = AreaDto.to(dto, entityManager);
        model = areaService.save(model);
        return JsonUtils.succeedMap(AreaDto.from(model, true));
    }
    
    @RequestMapping("/delete.json")
    @ResponseBody
    public Object delete(String id) {
        if (StringUtils.isBlank(id)) {
            return JsonUtils.failedMap("错误的参数！");
        }
        
        try {
            areaService.delete(id);
            return JsonUtils.succeedMap("");
        }
        catch (Exception e) {
            return JsonUtils.failedMap(e.getMessage());
        }
    }
    
    @RequestMapping("/tree.json")
    @ResponseBody
    public Object getAreaTree(String id, HttpServletRequest request) {
        try {
            List<Area> area = null;
            if (StringUtils.isBlank(id)) {
                area = areaFinder.listProvinces();
            }
            else {
                area = areaFinder.listCities(id);
            }
            
            List<AreaTreeNodeDto> dtos = new ArrayList<AreaTreeNodeDto>();
            for (Area organization : area) {
                dtos.add(AreaTreeNodeDto.from(organization, request));
            }
            
            return JsonUtils.succeedMap(dtos);
        }
        catch (Exception e) {
            return JsonUtils.failedMap(e.getMessage());
        }
    }
    
}
